package com.logic.modular.game.statistical.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.comment.pojo.domain.logic.domain.PlayerHero;
import com.logic.modular.game.battle.info.base.PvpPlayerInfo;
import com.logic.modular.game.player.hero.mapper.PlayerHeroMapper;
import com.logic.modular.game.player.skill.mapper.PlayerSkillMapper;
import com.logic.modular.game.statistical.PlayerStatisticalPoolService;
import com.logic.modular.game.statistical.domain.PlayerStatisticalPool;
import com.logic.modular.game.statistical.mapper.PlayerStatisticalPoolMapper;
import com.logic.modular.gameCore.cultivate.exp.CoreExperienceServiceImpl;
import com.logic.modular.gameCore.cultivate.hero.service.CoreHeroServiceImpl;
import com.logic.modular.gameCore.cultivate.hero.service.CoreHeroStarServiceImpl;
import com.logic.modular.gameCore.cultivate.skill.controller.CoreSkillStarServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;

/**
 * 玩家统计池(PlayerStatisticalPool)表服务实现类
 *
 * @author cxy
 * @since 2024-11-05 14:29:30
 */
@Slf4j
@Service
public class PlayerStatisticalPoolServiceImpl extends ServiceImpl<PlayerStatisticalPoolMapper, PlayerStatisticalPool>
        implements PlayerStatisticalPoolService {
    @Resource
    private CoreExperienceServiceImpl coreExperienceServiceImpl;
    @Resource
    private CoreHeroStarServiceImpl coreHeroStarServiceImpl;
    @Resource
    private PlayerHeroMapper playerHeroMapper;
    @Resource
    private CoreHeroServiceImpl coreHeroServiceImpl;
    @Resource
    private CoreSkillStarServiceImpl coreSkillStarServiceImpl;
    @Resource
    private PlayerSkillMapper playerSkillMapper;

    private PlayerStatisticalPool select(Long playerId) {
        return getOne(new LambdaQueryWrapper<PlayerStatisticalPool>().eq(PlayerStatisticalPool::getPlayerId, playerId));
    }

    public PlayerStatisticalPool obtain(long playerId) {
        PlayerStatisticalPool playerStatisticalPool = select(playerId);
        if (playerStatisticalPool == null || playerStatisticalPool.getConditionValue2().size() < 100) {
            playerStatisticalPool = new PlayerStatisticalPool();
            playerStatisticalPool.setPlayerId(playerId);
            playerStatisticalPool.conditionValue2.addAll(Collections.nCopies(100, 0L));
            saveOrUpdate(playerStatisticalPool);
        }
        // 刷新统计
        flushHeroInfo(playerId);
        flushSkillInfo(playerId);
        return playerStatisticalPool;
    }

    @Override
    public void refreshStatistical(PvpPlayerInfo pvpPlayerInfo) {
        PlayerStatisticalPool obtain = obtain(pvpPlayerInfo.getPlayerId());
        // 1	击败玩家 	击败玩家的最后一个城池
        int size = pvpPlayerInfo.getKilledPlayers().size();
        long l1 = obtain.conditionValue.get(1) + size;
        obtain.conditionValue2.add(1, l1);

        // 2	击杀敌军	击杀敌方的兵力数量
        Long killForces = pvpPlayerInfo.getKillForces();
        long l2 = obtain.conditionValue.get(2) + killForces;
        obtain.conditionValue2.add(2, l2);

        // 3	攻占城池	占领的城池数
        Integer maxCity = pvpPlayerInfo.getMaxCity();
        long l3 = obtain.conditionValue.get(3) + maxCity - 1;
        obtain.conditionValue2.add(3, l3);


        // 4	完成统一	统一
        Integer isRuler = pvpPlayerInfo.getIsRuler();
        long l4 = obtain.conditionValue.get(4) + isRuler;
        obtain.conditionValue2.add(4, l4);

        // 5	完成对局	完成对局
        Integer isExit = pvpPlayerInfo.getIsExit();
        long l5 = obtain.conditionValue.get(5) + isExit;
        obtain.conditionValue2.add(5, l5);

        // 6	累计游戏时间
        Long l6 = obtain.conditionValue2.get(6) + pvpPlayerInfo.obtainSurvivalTime();
        obtain.conditionValue2.add(6, l6);

        updateById(obtain);
    }


    @Override
    public void flushHeroInfo(Long playerId) {
        PlayerStatisticalPool obtain = select(playerId);

        Integer heroMaxLevel = coreExperienceServiceImpl.getMaxLevel(1);
        Integer heroMaxStar = coreHeroStarServiceImpl.getMaxStar();
        Integer heroMaxQualityLv = coreHeroServiceImpl.getHeroMaxQualityLv();

        long l20 = playerHeroMapper.countMaxLevelHero(playerId, heroMaxLevel);
        obtain.conditionValue2.add(20, l20);

        long l21 = playerHeroMapper.countMaxStarHero(playerId, heroMaxStar);
        obtain.conditionValue2.add(21, l21);

        long l22 = playerHeroMapper.countMaxQualityHero(playerId, heroMaxQualityLv);
        obtain.conditionValue2.add(22, l22);
    }


    @Override
    public void logRisingStar(PlayerHero playerHero) {
        PlayerStatisticalPool obtain = select(playerHero.getUid());
        Long l = obtain.conditionValue2.get(23);
        obtain.conditionValue2.add(23, l + 1);
    }


    @Override
    public void flushSkillInfo(Long playerId) {
        PlayerStatisticalPool obtain = select(playerId);
        Integer skillMaxStar = coreSkillStarServiceImpl.getMaxStar();

        long l30 = playerSkillMapper.count(playerId);
        obtain.conditionValue2.add(30, l30);

        long l31 = playerSkillMapper.countMaxStar(playerId, skillMaxStar);
        obtain.conditionValue2.add(31, l31);
    }
}
